import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

const router = new Router({
  routes: [
    { path: "/login", component: () => import("../views/login.vue") },
    { path: "/", redirect: "/home" },
    {
      path: "/main",
      name: "Main",
      component: () => import("../views/main.vue"),
      children: [
        {
          path: "/home",
          name: "home",
          component: () => import("../views/home.vue"),
        },
        {
          path: "/user",
          name: "user",
          component: () => import("../views/user.vue"),
        },
        {
          path: "/mall",
          name: "mall",
          component: () => import("../views/mall.vue"),
        },
        {
          path: "/page1",
          name: "page1",
          component: () => import("../views/PageOne.vue"),
        },
        {
          path: "/page2",
          name: "page2",
          component: () => import("../views/pageTwo.vue"),
        },
      ],
    },
  ],
});

router.beforeEach((to, from, next) => {
  if (to.path == "/login") return next();
  let token = window.sessionStorage.getItem("token");
  if (!token) return next("/login");
  next();
});
export default router;
